package com.stephen.redisdemo.config;

import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@MapperScan("com.stephen.redisdemo.dao")
@EnableTransactionManagement
public class MybatisPlusConfig {

    /**
     * 分页插件
     * @return
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

    /**
     * 乐观锁插件
     * @return
     */
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        return new OptimisticLockerInterceptor();
    }

    /**
     * sql性能分析 需要引入 mybatis-plus-extension 依赖才可以用
     * 如果 mybatis-plus-boot-starter 是3.0.5版本,则不用引入额外依赖
     * 高版本,需要重新引入额外依赖
     * 注意与 mybatis-plus-boot-starter 的版本兼容问题
     * @return
     */
    @Bean
    @Profile({"dev","test"}) // 设置只在 dev,test环境开启
    public PerformanceInterceptor performanceInterceptor() {
        PerformanceInterceptor p = new PerformanceInterceptor();
        p.setFormat(true); // 格式化sql显示
        return p;
    }

}